﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class VisaRestrictions : Form
    {
        public VisaRestrictions()
        {
            InitializeComponent();
        }

        private void VisaRestrictions_Load(object sender, EventArgs e)
        {
            lbxOnVisa.SelectedIndexChanged += (_sender, _e) =>
                {
                    btnTakeOff.Enabled = SelectedOnVisa >= 0;
                };
            cbxNotOnVisa.SelectedIndexChanged += (_sender, _e) =>
                { 
                    btnPutOn.Enabled = SelectedOffVisa >= 0;
                };
            ReloadListboxes();
            btnTakeOff.Enabled = SelectedOnVisa >= 0;
            btnPutOn.Enabled = SelectedOffVisa >= 0;
        }

        private Int64 SelectedOnVisa
        {
            get
            {
                if (lbxOnVisa.Items.Count == 0 ||
                    lbxOnVisa.SelectedItems.Count != 1 ||
                    lbxOnVisa.SelectedValue == null ||
                    lbxOnVisa.SelectedValue.GetType() != typeof(Int64))
                {
                    return -1;
                }
                return (Int64)lbxOnVisa.SelectedValue;
            }
        }

        private Int64 SelectedOffVisa
        {
            get
            {
                if (cbxNotOnVisa.Items.Count == 0 ||                    
                    cbxNotOnVisa.SelectedValue == null ||
                    cbxNotOnVisa.SelectedValue.GetType() != typeof(Int64))
                {
                    return -1;
                }
                return (Int64)cbxNotOnVisa.SelectedValue;
            }
        }

        private void ReloadListboxes()
        {
            lbxOnVisa.DataSource = null;
            lbxOnVisa.Items.Clear();
            lbxOnVisa.DataSource = SQLiteAccess.EmployeesOnVisa(true);
            lbxOnVisa.DisplayMember = "Value";
            lbxOnVisa.ValueMember = "Key";

            cbxNotOnVisa.DataSource = null;
            cbxNotOnVisa.Items.Clear();
            cbxNotOnVisa.DataSource = SQLiteAccess.EmployeesOnVisa(false);
            cbxNotOnVisa.DisplayMember = "Value";
            cbxNotOnVisa.ValueMember = "Key";
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }

        private void btnTakeOff_Click(object sender, EventArgs e)
        {
            SQLiteAccess.SetOnVisa(false, SelectedOnVisa);
            ReloadListboxes();
            Sounds.Play(Sounds.Validated);
        }

        private void btnPutOn_Click(object sender, EventArgs e)
        {
            SQLiteAccess.SetOnVisa(true, SelectedOffVisa);
            ReloadListboxes();
            Sounds.Play(Sounds.Validated);
        }

    }
}
